<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title></title>
  <link rel="stylesheet" href="/static/layui/css/layui.css"/>
</head>
<body>
<form class="layui-form" action="">

  <div class="layui-form-item">
    <label class="layui-form-label">用户管理</label>
    <div class="layui-input-block" id="check2">

    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">系统管理</label>
    <div class="layui-input-block" id="check1">

    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">医生管理</label>
    <div class="layui-input-block" id="check3">

    </div>
  </div>
</form>
<script src="/static/js/system.js"></script>
<script src="/static/js/jquery-3.3.1.js"></script>
<script src="/static/layui/layui.js"></script>
<script>
  //Demo
  layui.use(['form', 'laydate', 'jquery', 'layer'], function () {
    var form = layui.form;
    var laydate = layui.laydate;
    var $ = layui.jquery;
    var layer = layui.layer;
    $.ajax({
      url: '/RoleController/getAuthority',
      type: 'post',
      data: {"rid":getQueryString("rid")},
      dataType: 'json',
      success: function (rs) {
        console.log(rs);
        $.each(rs.date, function (i, o) {
          console.log(i);
          if (o.hierarchy == 1) {//系统管理
            $("#check1").append('<input type="checkbox" name="'
                    + o.pid + '" title="' + o.linkname + '" value="' + o.pid +
                    '" ' + (o.ck != -1 ? 'checked' : '') + '>');
          } else if (o.hierarchy == 2) {//用户管理
            $("#check2").append('<input type="checkbox" name="'
                    + o.pid + '" title="' + o.linkname + '" value="' + o.pid +
                    '" ' + (o.ck != -1 ? 'checked' : '') + '>');
          } else if (o.hierarchy == 1) {//用户管理
            $("#check3").append('<input type="checkbox" name="'
                    + o.pid + '" title="' + o.linkname + '" value="' + o.pid +
                    '" ' + (o.ck != -1 ? 'checked' : '') + '>');
          }
        });
        form.render();
      }
    })

    //监听提交
    form.on('checkbox', function (data) {
      console.log(data.value);
      console.log(data.elem.checked)
      $.ajax({
        url: '/AuthorityServlet?method=upd',
        data: {"id": data.value, "type": data.elem.checked, "rid": parent.param.id},
        type: 'post',
        dataType: 'json',
        success: function (rs) {
          if (rs.code == 200) {
            layer.msg(data.elem.checked ? "权限添加成功" : "权限删除成功");
          } else {
            layer.error(data.elem.checked ? "权限添加失败" : "权限删除失败");
          }
        }
      })
      return false;
    });

    laydate.render({
      elem: '#test1' //指定元素
    });
  });
</script>
</body>
</html>
